// function to set a cookie
document.write ( '<script type="text/javascript" src="http://s1.hero.9wee.com/api/status/init_server_data.php?callback=init_game_servers"><' + '/script>' );
	
function setCookie ( name, value )
{
	expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

// function to retrieve a cookie
function getCookie ( name )
{
	cookie_name = name + "=";
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (";", value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}

// function to delete a cookie
function delCookie ( name )
{
	var expireNow = new Date();
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

// 开设多区时使用该函数显示选服窗口并 return false
start_game = function ()
{
	return true;
};

// 获取 URL 变量
function getURLVar ( queryVar, url )
{
	var url = url == null ? window.location.href : url;
	var re = new RegExp ( ".*[\?|&]" + queryVar + "=([^&#]+).*", "g" );
	if ( url.match ( re ) )
	{
		var queryVal = url.replace ( re, "$1" );
		return queryVal;
	}
	return '';
}

// 生成服务器列表
init_game_servers = function( ret, backurl )
{
	var qStr = '';
	var recommend = '';

	var unionQuery = getURLVar ( 'q' );
	if ( unionQuery == "" ) unionQuery = getCookie ( 'cookie_q' );
	if ( unionQuery ) qStr = '&q=' + unionQuery;

	var cookieLastSvrName = getCookie ( 'heroLastSvrName' );

	var html = '<ul>';
	for ( var i in ret )
	{
		if ( !ret[i].s_name ) continue;

		var serverClass = 'main_ban';

		if ( ret[i].is_recommend )
		{
			serverClass = 'main_ban_new';
		}

		var onlineCount = '在线：' + ret[i].onlie_count;
		var target = '';
		
		if ( parseInt ( ret[i].stopped ) > 0 )
		{
			if ( ret[i].start_time )
			{
				var onclick = "alert ( '该服务器将于 " + ret[i].start_time + " 开放！' ); return false;";
				onlineCount = ret[i].start_time + ' 开放';
			}
			else
			{
				var onclick = "alert ( '该服务器当前正在停服维护，请稍后访问！' ); return false;";
				onlineCount = '停服中';
			}

			var href = 'javascript:;';
			serverClass = 'main_ban_stop';
		}
		else if ( parseInt ( ret[i].count_all ) >= parseInt ( ret[i].count_limit ) )
		{
			var href = 'javascript:;';
			var onclick = "alert ( '很抱歉，该游戏服务区注册人数已满，暂时无法激活！\n我们会对服务器进行小号和死号的清理并进行扩容后重新开放！\n您可以先行选择其他游戏服务区进行游戏！' ); return false;";
		}
		else
		{
			var url = ret[i].s_url;
			if ( url.substr ( url.length - 1, 1 ) != '/' ) url += '/';

			var href = url + 'passport.php?act=login&backurl='+backurl+'&referer=/' + qStr;
			var onclick = "setCookie ( 'heroLastSvrName', '" + ret[i].s_name + "' );";
			var target = '_blank';
		}

		if ( ret[i].s_name == cookieLastSvrName )
		{
			serverClass = 'main_ban_last';
		}

		html += '<li class="' + serverClass + '">';
		html += '	<span class="ser_name"><a href="' + href + '" target="' + target + '" onclick="' + onclick + '">' + ret[i].s_name + '</a></span><span class="online">' + onlineCount + '</span>' + recommend;
		html += '</li>';
	}
	html += '</ul>';

	document.getElementById('game_servers').innerHTML = html;
};

